<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="88px">
      <el-form-item label="套餐名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入套餐名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备状态" prop="status">
         <el-select v-model="queryParams.status" clearable>
          <el-option
            v-for="item in statusList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetQuery">重置</el-button>
        <el-button type="primary" @click="handleQuery">查询</el-button>
      </el-form-item>
    </el-form>
    <el-form size="small">
      <el-form-item>
        <el-button type="primary" @click="add">新增套餐</el-button>
        <el-button @click="delbatch" style="border: 1px solid #FF5555;color:#FF5555">删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="list"
      style="width: 100%;"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="50"></el-table-column>
      <el-table-column label="序号" type="index" >
        <template slot-scope="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="套餐名称"  prop="name" />
      <el-table-column label="充值金额">
        <template slot-scope="scope">
          {{scope.row.rechargeAmount ? Number(scope.row.rechargeAmount).toFixed(2) : '0'}}
        </template>
      </el-table-column>
      <el-table-column label="赠送金额">
        <template slot-scope="scope">
          {{scope.row.giftAmount ? Number(scope.row.giftAmount).toFixed(2) : '0'}}
        </template>
      </el-table-column>
      <el-table-column label="设备状态" >
        <template slot-scope="scope">
          <span v-show="scope.row.status == '1'" style="color:#386BD7">激活</span>
          <span v-show="scope.row.status == '0'">未激活</span>
        </template>
      </el-table-column>
      <el-table-column label="说明"  prop="remark" />
      <el-table-column label="操作" >
        <template slot-scope="scope">
          <el-button type="text" style="color:#386BD7" @click="edit(scope.row)">编辑</el-button>
          <el-button type="text" style="color:#FF5555" @click="del(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
    <!-- 添加 修改终端 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="套餐名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入" style="width:90%"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="充值金额" prop="rechargeAmount">
              <el-input v-model="form.rechargeAmount" @input="val=>form.rechargeAmount=val.replace(/[^\d]/g,'')" placeholder="请输入" style="width:90%"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="赠送金额" prop="giftAmount">
              <el-input v-model="form.giftAmount" @input="val=>form.giftAmount=val.replace(/[^\d]/g,'')" placeholder="请输入" style="width:90%"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="套餐状态" prop="status">
              <el-select v-model="form.status" clearable style="width:90%">
                <el-option
                  v-for="item in statusList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="说明" prop="remark">
            <el-input type="textarea" v-model="form.remark" placeholder="请输入" resize="none" :rows="4"></el-input>
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="open = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog
    title="提示"
    :visible.sync="delVisible"
    width="30%">
    <span>是否删除？</span>
    <span slot="footer" class="dialog-footer">
      <el-button @click="delVisible = false">取 消</el-button>
      <el-button type="primary" @click="delConfirm">确 定</el-button>
    </span>
  </el-dialog>
  </div>
</template>

<script>
import { getPackageList,addPackage,updatePackage,delPackage,packageDetail } from "@/api/common";

export default {
  name: "Online",
  data() {
    return {
      delVisible:false,
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      statusList:[
        {value:'1',label:'激活'},
        {value:'0',label:'未激活'},
      ],
      selection:[],
      form:{},
      rules: {
        name: [
          { required: true, message: "套餐名称不能为空", trigger: "blur" }
        ],
        rechargeAmount: [
          { required: true, message: "充值金额不能为空", trigger: "blur" }
        ],
        giftAmount: [
          { required: true, message: "赠送金额不能为空", trigger: "blur" }
        ],
        status:[
          { required: true, message: "套餐状态不能为空", trigger: "blur" }
        ],
      },
      title:'',
      open:false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      getPackageList(this.queryParams).then(response => {
        this.list = response.rows
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams =  {
        pageNum: 1,
        pageSize: 10
      }
      this.handleQuery();
    },
    handleSelectionChange(val){
      this.selection = val
    },
    add(){
      this.form = {}
      this.open = true;
      this.title = "新增套餐";
    },
    edit(row){
      this.form = row
      this.open = true;
      this.title = "编辑套餐";
    },
    del(row){
      this.selection = [row]
      this.delVisible = true
    },
    delbatch(){
      if(!this.selection.length){
        this.$message.warning('请选择一个套餐')
        return
      }
      this.delVisible = true
    },
    delConfirm(){
      this.delVisible = false
      let ids = this.selection.map(e=>e.id).join(',')
      delPackage(ids).then(res=>{
        if(res.code == 200){
          this.$message.success('删除成功')
          this.getList()
        }
      })
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updatePackage(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPackage(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
  }
};
</script>

<style lang="scss" scoped>
.warnStatus{
  display: flex;
  justify-content: center;
}
.circle{
  width: 8px;
  height: 8px;
  border-radius: 50%;
}
.terminalContent{
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 40px;
  .terminalItem{
    width: 50%;
  }
}
.dealContent{
  display: flex;
  align-items: center;
  height: 40px;
}
</style>

